package com.gwsdk.paysdk.acitvityV2.fragment;

import android.annotation.SuppressLint;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.gwsdk.paysdk.acitvityV2.GWMainMenuActivity;
import com.gwsdk.paysdk.acitvityV2.adapter.GWPacksAdapter;
import com.gwsdk.paysdk.common.Constant;
import com.gwsdk.paysdk.entity.GamePackInfo;
import com.gwsdk.paysdk.entity.PacksInfo;
import com.gwsdk.paysdk.http.process.GamePacksListProcess;
import com.gwsdk.paysdk.http.process.PacksCodeProcess;
import com.gwsdk.paysdk.utils.GWInflaterUtils;
import com.gwsdk.paysdk.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

public class GWGameGiftFragment extends GWBaseFragment{


    private ListView listView;
    private GWPacksAdapter mPacksAdapter;//xlistview的适配器
    private List<GamePackInfo> packList = new ArrayList<>();
    private GamePacksListProcess gamePacksListProcess;
    private View loadingView;
    private GamePackInfo packInfo;
    private View notDataView;
    private TextView giftBtn;
    private TextView supGiftBtn;
    private int type = 0;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int setContentView() {
        return GWInflaterUtils.getLayout(getActivity(),"gw_fragment_game_gift");
    }

    @Override
    public void initView(View view) {
        loadingView = view.findViewById(GWInflaterUtils.getControl(getActivity(),"loading_view"));
        giftBtn = view.findViewById(GWInflaterUtils.getControl(getActivity(),"gw_gift_btn"));
        supGiftBtn = view.findViewById(GWInflaterUtils.getControl(getActivity(),"gw_sup_gift_btn"));

        giftBtn.setSelected(true);
        supGiftBtn.setSelected(false);

        listView = view.findViewById(GWInflaterUtils.getControl(getActivity(),"list_view"));
        mPacksAdapter = new GWPacksAdapter(getActivity());
        notDataView = view.findViewById(GWInflaterUtils.getControl(getActivity(),"not_data_view"));
        listView.setAdapter(mPacksAdapter);

        mPacksAdapter.setReceiveClick(new GWPacksAdapter.OnReceiveClick() {
            @Override
            public void onClick(GamePackInfo p) {
                packInfo = p;
                if (packInfo.getPackStatus().equals("1")) {
                    ClipboardManager cmb = (ClipboardManager)getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
                    cmb.setText(packInfo.getPackCode());
                    ToastUtil.show(getActivity(),"复制成功");
                }else {
                    setPackInfo();
                }
            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                GamePackInfo gamePackInfo = packList.get(i);
                GWMainMenuActivity activity = (GWMainMenuActivity) getActivity();

                GWGiftDetailsFragment detailsFragment = new GWGiftDetailsFragment();
                Bundle bundle = new Bundle();
                bundle.putSerializable("packInfo",gamePackInfo);
                detailsFragment.setArguments(bundle);

                activity.addOtherFragment(detailsFragment);
            }
        });

        giftBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (giftBtn.isSelected()) return;
                giftBtn.setSelected(true);
                supGiftBtn.setSelected(false);
                giftBtn.setTextColor(Color.parseColor("#14B6FC"));
                supGiftBtn.setTextColor(Color.parseColor("#E4E4E4"));
                getData();
            }
        });
        supGiftBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (supGiftBtn.isSelected()) return;
                giftBtn.setSelected(false);
                supGiftBtn.setSelected(true);
                supGiftBtn.setTextColor(Color.parseColor("#14B6FC"));
                giftBtn.setTextColor(Color.parseColor("#E4E4E4"));
                getData();
            }
        });

    }

    public void setPackInfo() {

        PacksCodeProcess packCode = new PacksCodeProcess();
        packCode.setGiftId(packInfo.getGiftId());
        packCode.post(handler);
    }

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case Constant.PACKS_CODE_FAIL:
                    if((String) msg.obj!=null){
                        ToastUtil.show(getActivity(),(String) msg.obj);
                    }
                    break;
                case Constant.PACKS_CODE_SUCCESS:
                    getData();
                    break;
            }
        }
    };

    @Override
    public void getData() {
        //普通礼包数据
        if(gamePacksListProcess==null){
            gamePacksListProcess = new GamePacksListProcess();
        }
        loadingView.setVisibility(View.VISIBLE);

        if (giftBtn.isSelected()){
            gamePacksListProcess.post(mHandler);
        }else {
            gamePacksListProcess.post1(mHandler);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    private PacksInfo packsInfo;
    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {

        @SuppressLint("NewApi")
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            loadingView.setVisibility(View.GONE);

            switch (msg.what) {
                case Constant.GET_PACKS_LIST_SUCCESS:  //获取普通礼包列表成功
                    packsInfo = (PacksInfo) msg.obj;
                    if (null != packsInfo.getPackInfoList() && packsInfo.getPackInfoList().size() > 0) {
                        listView.setVisibility(View.VISIBLE);
                        notDataView.setVisibility(View.GONE);
                        packList.clear();
                        packList.addAll(packsInfo.getPackInfoList());
                        mPacksAdapter.setPackList(packList);
                    } else {
                        listView.setVisibility(View.GONE);
                        notDataView.setVisibility(View.VISIBLE);
                    }
                    break;
                case Constant.GET_PACKS_SUP_LIST_SUCCESS:
                    packsInfo = (PacksInfo) msg.obj;
                    if (null != packsInfo.getPackInfoList() && packsInfo.getPackInfoList().size() > 0) {
                        listView.setVisibility(View.VISIBLE);
                        notDataView.setVisibility(View.GONE);
                        packList.clear();
                        packList.addAll(packsInfo.getPackInfoList());
                        mPacksAdapter.setPackList(packList);
                    } else {
                        listView.setVisibility(View.GONE);
                        notDataView.setVisibility(View.VISIBLE);
                    }
                    break;
                default:
                    break;
            }
        }
    };
}
